package com.producer.queue.dealing;

import com.producer.queue.bean.DataBean;
import com.producer.service.impl.OutPutServiceImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;



/**
 * @Author : liu lin qin
 * @Date :  Created by pangoin on 2019/8/26.
 */
@Component("outPutDealing")
public class OutPutDealing {
    private static final Log logger = LogFactory.getLog(OutPutServiceImpl.class);
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;


    public void send(DataBean dataBean) {
        ListenableFuture<SendResult<String, String>> result;

        result = kafkaTemplate.send(dataBean.getTopic(), dataBean.getMsg());

        result.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
            @Override
            public void onSuccess(SendResult<String, String> result) {
                logger.info("OutPutDealing Message send success:" + result);
            }

            @Override
            public void onFailure(Throwable ex) {
                logger.error("OutPutDealing Message send error :" + ex.getMessage());
            }
        });
    }
}

